import { defineStore } from "pinia";
import { getUserInfo } from "@/api/userInfo.js";

export const useUserInfo = defineStore("userinfo", {
  state: () => {
    return {
        id: "",
      imageUrl: "",
      name: "",
      sex: "",
      department: "",
      identity: "",
      account: "",
    };
  },
  actions: {
    async userInfo(id) {
      const res = await getUserInfo({ id: id });
      this.id = res.data.data.id;
      this.imageUrl = res.data.data.image_url;
      this.name = res.data.data.name;
      this.sex = res.data.data.sex;
      this.department = res.data.data.department;
      this.identity = res.data.data.identity;
      this.account = res.data.data.account;
      this.email = res.data.data.email;
    },
    updateUserInfo(data) {
        this.id = data.id;
        this.imageUrl = data.image_url;
        this.name = data.name;
        this.sex = data.sex;
        this.department = data.department;
        this.identity = data.identity;
        this.account = data.account;
        this.email = data.email;
    }
  },
  persist: {
    enabled: true, // 启用持久化
    key: "userinfo", // 存储在localStorage中的键
    storage: localStorage, // 使用localStorage存储
  },
});
